/*
 * @(#)DrawEdgeAss.java   09/09/13
 *
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 *
 *
 *
 */



package fr.ema.arch3d.gea.ass;

import fr.ema.arch3d.gea.port.MyPort;

import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.GraphConstants;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.util.Fractal;

import java.awt.Color;

import java.util.ArrayList;
import java.util.List;

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class DrawEdgeAss {
    public List drawEdge(DefaultGraphCell[] cells, Component connector) {
        List           es        = new ArrayList();
        boolean        evolution = false;
        ConnectorEdges ctredges  = new ConnectorEdges(connector);
        MyVertexAss    cellC     = null,
                       cellS     = null;
        MyVertexAss    cellC1    = null,
                       cellS1    = null;

        // ports of components
        MyPort portC  = null,
               portS  = null;
        MyPort portC1 = null,
               portS1 = null;

        // ports of connectors
        MyPort      portRC  = null,
                    portPC  = null,
                    portRC1 = null;
        DefaultEdge er      = new DefaultEdge(),
                    ep      = new DefaultEdge(),
                    ee      = new DefaultEdge();

        for (int j = 0; j < cells.length; j++) {
            try {
                if (((MyVertexAss) cells[j]).vertexName.equals(Fractal.getNameController(connector).getFcName())) {
                    for (int i = 0; i < cells[j].getChildCount(); i++) {
                        if (((MyPort) cells[j].getChildAt(i)).portName.equals("r-I")) {
                            portRC = (MyPort) cells[j].getChildAt(i);
                        }

                        if (((MyPort) cells[j].getChildAt(i)).portName.equals("r-II")) {
                            portRC1 = (MyPort) cells[j].getChildAt(i);

                            // System.out.println(portRC1.portName+Fractal.getNameController(connector).getFcName());
                        }

                        if (((MyPort) cells[j].getChildAt(i)).portName.equals("p")) {
                            portPC = (MyPort) cells[j].getChildAt(i);
                        }
                    }
                }
            } catch (NoSuchInterfaceException e) {

                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        for (int i = 0; i < cells.length; i++) {
            try {
                if (((MyVertexAss) cells[i]).vertexName.equals(Fractal.getNameController(ctredges.cc).getFcName())) {
                    cellC = (MyVertexAss) cells[i];

                    // System.out.println(cellC.vertexName);
                } else if (((MyVertexAss) cells[i]).vertexName.equals(
                        Fractal.getNameController(ctredges.cs).getFcName())) {
                    cellS = (MyVertexAss) cells[i];

                    // System.out.println(cellS.vertexName);
                }

                if (ctredges.cc1 != null) {
                    evolution = true;

                    if (((MyVertexAss) cells[i]).vertexName.equals(
                            Fractal.getNameController(ctredges.cc1).getFcName())) {
                        cellC1 = (MyVertexAss) cells[i];
                        System.out.println("cc=" + cellC1.vertexName);
                    }
                }

                if (ctredges.cs1 != null) {
                    evolution = true;

                    if (((MyVertexAss) cells[i]).vertexName.equals(
                            Fractal.getNameController(ctredges.cs1).getFcName())) {
                        cellS1 = (MyVertexAss) cells[i];
                        System.out.println("cs=" + cellS1.vertexName);
                    }
                }
            } catch (NoSuchInterfaceException e) {

                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        for (int i = 0; i < cellC.getChildCount(); i++) {
            if (((MyPort) cellC.getChildAt(i)).portName.equals(ctredges.client_itf)) {
                portC = (MyPort) cellC.getChildAt(i);
            }
        }

        for (int i = 0; i < cellS.getChildCount(); i++) {
            if (((MyPort) cellS.getChildAt(i)).portName.equals(ctredges.server_itf)) {
                portS = (MyPort) cellS.getChildAt(i);
            }
        }

        if (cellC1 != null) {
            for (int i = 0; i < cellC1.getChildCount(); i++) {
                if (((MyPort) cellC1.getChildAt(i)).portName.equals(ctredges.client_itf1)) {
                    portC1 = (MyPort) cellC1.getChildAt(i);
                }
            }
        }

        if (cellS1 != null) {
            for (int i = 0; i < cellS1.getChildCount(); i++) {
                if (((MyPort) cellS1.getChildAt(i)).portName.equals(ctredges.server_itf1)) {
                    portS1 = (MyPort) cellS1.getChildAt(i);
                }
            }
        }

        // System.out.println(portC.portName+portS.portName);
        DefaultEdge e = new DefaultEdge();

        e.setSource(portC);
        e.setTarget(portS);
        GraphConstants.setLineColor(ep.getAttributes(), Color.green);
        GraphConstants.setLineColor(er.getAttributes(), Color.magenta);

        if (evolution) {
            er.setSource(portC);
            er.setTarget(portPC);
            ep.setSource(portRC);
            ep.setTarget(portS);

            if (cellC1 != null) {
                ee.setSource(portC1);
                ee.setTarget(portPC);
                GraphConstants.setLineColor(ee.getAttributes(), Color.red);

                float[] d = { 5, 5 };

                // GraphConstants.setDashOffset(ee.getAttributes(), 100);
                GraphConstants.setDashPattern(ee.getAttributes(), d);
                GraphConstants.setLineWidth(ee.getAttributes(), 2);
            }

            if (cellS1 != null) {
                ee.setSource(portRC);
                ee.setTarget(portS1);
            }

            es.add(er);
            es.add(ep);
            es.add(ee);
        } else {
            er.setSource(portC);
            er.setTarget(portPC);
            ep.setSource(portRC);
            ep.setTarget(portS);
            es.add(er);
            es.add(ep);
        }

        GraphConstants.setEndFill(ctredges.getAttributes(), true);

        /*
         * try {
         *       GraphConstants.setValue(edge.getAttributes(), Fractal.getNameController(connector).getFcName());
         * } catch (NoSuchInterfaceException e) {
         *       // TODO Auto-generated catch block
         *       e.printStackTrace();
         * }
         */
        return es;
    }

    public List drawEdge(DefaultGraphCell[] cells, Component connector, String newCmpName) {
        List           es        = new ArrayList();
        boolean        evolution = false;
        ConnectorEdges ctredges  = new ConnectorEdges(connector, newCmpName);
        MyVertexAss    cellC     = null,
                       cellS     = null;
        MyVertexAss    cellC1    = null,
                       cellS1    = null;

        // ports of components
        MyPort portC  = null,
               portS  = null;
        MyPort portC1 = null,
               portS1 = null;

        // ports of connectors
        MyPort      portRC  = null,
                    portPC  = null,
                    portRC1 = null;
        DefaultEdge er      = new DefaultEdge(),
                    ep      = new DefaultEdge(),
                    ee      = new DefaultEdge();

        for (int j = 0; j < cells.length; j++) {
            try {
                if (((MyVertexAss) cells[j]).vertexName.equals(Fractal.getNameController(connector).getFcName())) {
                    for (int i = 0; i < cells[j].getChildCount(); i++) {
                        if (((MyPort) cells[j].getChildAt(i)).portName.equals("r-I")) {
                            portRC = (MyPort) cells[j].getChildAt(i);
                        }

                        if (((MyPort) cells[j].getChildAt(i)).portName.equals("r-II")) {
                            portRC1 = (MyPort) cells[j].getChildAt(i);

                            // System.out.println(portRC1.portName+Fractal.getNameController(connector).getFcName());
                        }

                        if (((MyPort) cells[j].getChildAt(i)).portName.equals("p")) {
                            portPC = (MyPort) cells[j].getChildAt(i);
                        }
                    }
                }
            } catch (NoSuchInterfaceException e) {

                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        for (int i = 0; i < cells.length; i++) {
            try {
                if (((MyVertexAss) cells[i]).vertexName.equals(Fractal.getNameController(ctredges.cc).getFcName())) {
                    cellC = (MyVertexAss) cells[i];

                    // System.out.println(cellC.vertexName);
                } else if (((MyVertexAss) cells[i]).vertexName.equals(
                        Fractal.getNameController(ctredges.cs).getFcName())) {
                    cellS = (MyVertexAss) cells[i];

                    // System.out.println(cellS.vertexName);
                }

                if (ctredges.cc1 != null) {
                    evolution = true;

                    if (((MyVertexAss) cells[i]).vertexName.equals(
                            Fractal.getNameController(ctredges.cc1).getFcName())) {
                        cellC1 = (MyVertexAss) cells[i];
                        System.out.println("cc=" + cellC1.vertexName);
                    }
                }

                if (ctredges.cs1 != null) {
                    evolution = true;

                    if (((MyVertexAss) cells[i]).vertexName.equals(
                            Fractal.getNameController(ctredges.cs1).getFcName())) {
                        cellS1 = (MyVertexAss) cells[i];
                        System.out.println("cs=" + cellS1.vertexName);
                    }
                }
            } catch (NoSuchInterfaceException e) {

                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        for (int i = 0; i < cellC.getChildCount(); i++) {
            if (((MyPort) cellC.getChildAt(i)).portName.equals(ctredges.client_itf)) {
                portC = (MyPort) cellC.getChildAt(i);
            }
        }

        for (int i = 0; i < cellS.getChildCount(); i++) {
            if (((MyPort) cellS.getChildAt(i)).portName.equals(ctredges.server_itf)) {
                portS = (MyPort) cellS.getChildAt(i);
            }
        }

        if (cellC1 != null) {
            for (int i = 0; i < cellC1.getChildCount(); i++) {
                if (((MyPort) cellC1.getChildAt(i)).portName.equals(ctredges.client_itf1)) {
                    portC1 = (MyPort) cellC1.getChildAt(i);
                }
            }
        }

        if (cellS1 != null) {
            for (int i = 0; i < cellS1.getChildCount(); i++) {
                if (((MyPort) cellS1.getChildAt(i)).portName.equals(ctredges.server_itf1)) {
                    portS1 = (MyPort) cellS1.getChildAt(i);
                }
            }
        }

        // System.out.println(portC.portName+portS.portName);
        DefaultEdge e = new DefaultEdge();

        e.setSource(portC);
        e.setTarget(portS);
        GraphConstants.setLineColor(ep.getAttributes(), Color.green);
        GraphConstants.setLineColor(er.getAttributes(), Color.magenta);

        if (evolution) {
            er.setSource(portC);
            er.setTarget(portPC);
            ep.setSource(portRC);
            ep.setTarget(portS);

            if (cellC1 != null) {
                ee.setSource(portC1);
                ee.setTarget(portPC);
                GraphConstants.setLineColor(ee.getAttributes(), Color.red);

                float[] d = { 5, 5 };

                // GraphConstants.setDashOffset(ee.getAttributes(), 100);
                GraphConstants.setDashPattern(ee.getAttributes(), d);
                GraphConstants.setLineWidth(ee.getAttributes(), 2);
            }

            if (cellS1 != null) {
                ee.setSource(portRC);
                ee.setTarget(portS1);
                GraphConstants.setLineColor(ee.getAttributes(), Color.red);

                float[] d = { 5, 5 };

                // GraphConstants.setDashOffset(ee.getAttributes(), 100);
                GraphConstants.setDashPattern(ee.getAttributes(), d);
                GraphConstants.setLineWidth(ee.getAttributes(), 2);
            }

            es.add(er);
            es.add(ep);
            es.add(ee);
        } else {
            er.setSource(portC);
            er.setTarget(portPC);
            ep.setSource(portRC);
            ep.setTarget(portS);
            es.add(er);
            es.add(ep);
        }

        GraphConstants.setEndFill(ctredges.getAttributes(), true);

        /*
         * try {
         *       GraphConstants.setValue(edge.getAttributes(), Fractal.getNameController(connector).getFcName());
         * } catch (NoSuchInterfaceException e) {
         *       // TODO Auto-generated catch block
         *       e.printStackTrace();
         * }
         */
        return es;
    }
}
